package ar.edu.utn.frgp.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ar.edu.utn.frgp.model.ReservaHecha;
import ar.edu.utn.frgp.servicio.PaqueteService;
import ar.edu.utn.frgp.servicio.ReservaHechaService;;

public class ReservasHechasServlet  extends GenericServlet  {
	private static final long serialVersionUID = 1L;
	private ReservaHechaService resService;
	private PaqueteService paqServise;
	
	public ReservasHechasServlet(){
		super();
		resService = new ReservaHechaService();
		paqServise = new PaqueteService();
	}
	
	
	@Override
	public void doAction(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		try {
			List<ReservaHecha> reserv = resService.getReservasHechas();
			request.setAttribute("reserv", reserv);
			request.getRequestDispatcher("/reservasHechas.jsp").forward(request, response);
	
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		try {
			String[] idRes = request.getParameterValues("idReserva");
			for(String id : idRes ){
				paqServise.acualizarDisponibles(resService.ObternerPaquete(id), "+");
				resService.eliminarReserva(id);				
			}
			response.sendRedirect(request.getContextPath() + "/reservasHechas");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}	
}
